﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK19.DB;

namespace PTTK19.GUI
{
    public partial class ChiTietNguyenLieuFrm : Form
    {
        System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["MonAnFrm"];
        public ChiTietNguyenLieuFrm()
        {
            InitializeComponent();
            loadThongTinNguyenLieu();
        }
        //load thong tin nguyen lieu
        public void loadThongTinNguyenLieu()
        {
            DataTable dt = DBNguyenLieu.getListNguyenLieu(int.Parse(((MonAnFrm)f).txtMaMonAn.Text));
            dgvDanhSachNguyenLieu.DataSource = dt;
            DataTable dt1 = DBNguyenLieu.getListNguyenLieuTheoMonAn(int.Parse(((MonAnFrm)f).txtMaMonAn.Text));
            dgvDanhSachNguyenLieuChoMonAn.DataSource = dt1;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                int _MaMonAn = int.Parse(((MonAnFrm)f).txtMaMonAn.Text);
                int _MaNguyenLieu = int.Parse(txtMaNguyenLieu.Text);
                int _SoLuong = int.Parse(txtSoLuong.Text);
                if (DBNguyenLieu.KiemTraTonKho(_MaNguyenLieu) == 1)
                {
                    DBChiTietNguyenLieuMonAn.CapNhatSoLuong(_MaMonAn, _MaNguyenLieu, _SoLuong);
                    MessageBox.Show("Cập nhật số lượng thành công!", "Thông báo");
                    loadThongTinNguyenLieu();
                }
                else
                {
                    DialogResult result3 = MessageBox.Show("Đã hết nguyên liệu, bạn có muốn lập phiếu yêu cầu nhập hàng không?",
                    "Thông báo",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2);
                    if (result3 ==  DialogResult.Yes)
                    {
                        LapPhieuYeuCau frm = new LapPhieuYeuCau();
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có thể số lượng bạn nhập quá lớn.", "Thông báo");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                int _MaMonAn = int.Parse(((MonAnFrm)f).txtMaMonAn.Text);
                int _MaNguyenLieu = 0;
                string _DonViTinh = null;
                int index = dgvDanhSachNguyenLieu.CurrentRow.Index;
                if (dgvDanhSachNguyenLieu.CurrentRow != null)
                {
                    //lay thong tin chi tiet nguyen lieu
                    _MaNguyenLieu = (int)dgvDanhSachNguyenLieu.Rows[index].Cells[0].Value;
                    _DonViTinh = dgvDanhSachNguyenLieu.Rows[index].Cells[2].Value.ToString();
                }
                DBChiTietNguyenLieuMonAn.ThemNguyenLieuVaoMonAn(_MaMonAn, _MaNguyenLieu, _DonViTinh);
                loadThongTinNguyenLieu();
            }
            catch (Exception ex)
            { 
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                int _MaMonAn = int.Parse(((MonAnFrm)f).txtMaMonAn.Text);
                int _MaNguyenLieu = 0;
                int index = dgvDanhSachNguyenLieuChoMonAn.CurrentRow.Index;
                if (dgvDanhSachNguyenLieuChoMonAn.CurrentRow != null)
                {
                    //lay thong tin chi tiet nguyen lieu
                    _MaNguyenLieu = (int)dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[0].Value;
                }
                DBChiTietNguyenLieuMonAn.BotNguyenLieuChoMonAn(_MaMonAn, _MaNguyenLieu);
                loadThongTinNguyenLieu();
            }
            catch (Exception ex)
            { }
        }

        private void dgvDanhSachNguyenLieuChoMonAn_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow currentrow = dgvDanhSachNguyenLieuChoMonAn.CurrentRow;
                if (currentrow == null)
                {
                    return;
                }
                int index = dgvDanhSachNguyenLieuChoMonAn.CurrentRow.Index;
                if (dgvDanhSachNguyenLieuChoMonAn.CurrentRow != null)
                {
                    //lay thong tin chi tiet nguyen lieu
                    int _MaNguyenLieu = (int)dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[0].Value;
                    string _TenNguyenLieu = dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[1].Value.ToString();
                    string _DonViTinh = dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[2].Value.ToString();
                    string _SoLuong = dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[3].Value.ToString();
                    txtMaNguyenLieu.Text = _MaNguyenLieu.ToString();
                    txtSoLuong.Text = _SoLuong;
                    txtTenNguyenLieu.Text = _TenNguyenLieu;
                    cbxDonViTinh.Text = _DonViTinh;
                }
            }
            catch (Exception ex)
            { }
        }

        private void dgvDanhSachNguyenLieu_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow currentrow = dgvDanhSachNguyenLieuChoMonAn.CurrentRow;
                if (currentrow == null)
                {
                    return;
                }
                int index = dgvDanhSachNguyenLieuChoMonAn.CurrentRow.Index;
                if (dgvDanhSachNguyenLieuChoMonAn.CurrentRow != null)
                {
                    //lay thong tin chi tiet nguyen lieu
                    int _MaNguyenLieu = (int)dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[0].Value;
                    string _TenNguyenLieu = dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[1].Value.ToString();
                    string _DonViTinh = dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[2].Value.ToString();
                    string _SoLuong = dgvDanhSachNguyenLieuChoMonAn.Rows[index].Cells[3].Value.ToString();
                    txtMaNguyenLieu.Text = _MaNguyenLieu.ToString();
                    txtSoLuong.Text = _SoLuong;
                    txtTenNguyenLieu.Text = _TenNguyenLieu;
                    cbxDonViTinh.Text = _DonViTinh;
                }
            }
            catch (Exception ex)
            { }
        }
    }
}
